from flask import Blueprint, render_template, jsonify, request
from models.entity.area import Area
from models.entity.sensor_type import SensorType
from setting import API_VERSION
import json
from utils.nodetree import get_all_area
# from utils.result import result
from utils.api_result import result

mod = Blueprint("basedata", __name__, url_prefix="/api/" + API_VERSION + "/base", template_folder="templates", static_folder="static")

# 获取区域的数据
@mod.route('/get_area', methods=['GET','POST'])
def get_area():
    """
    {
    "parent_id" : 120000
    }   爱上对方答复dfdsffdsa
    """
    if request.method == 'POST':
        try:
            # data = json.loads(request.form.decode("utf-8"))
            pid = request.form['pid']
            # print('data', data)
        except:
            return result(success=0, code=0, msg='数据格式错误')
        # pid = data.get('pid')
        areas = Area.get_areas_by_parent_id(pid)
        # print(province_id)
        tmp = {}
        data = []
        for item in areas:
            data.append({"id": item.area_id, "name": item.name,})
        tmp['success'] = 1
        tmp['code'] = 0
        tmp['msg'] = ""
        tmp['data'] = data
        return jsonify(tmp)
        # return result(success=1,code=1,msg=None, )
    else:
        return result(success=0,code=0,msg='请求方式错误', data=[])



@mod.route('/get_sensor_type', methods=['GET', 'POST'])
def get_device_type():
    """获取所有传感器类型"""
    """
        无需传入参数
    """
    if request.method == 'POST' or 'GET':
        # try:
        #     data = json.loads(request.data.decode("utf-8"))
        #     print('data', data)
        # except:
        #     return result(success=0, code=-1, msg='数据格式错误')
        sensor_types = SensorType.get_sensor_type_all()
        t_list = [item.get_dict() for item in sensor_types]
        return result(success=1, data=t_list)